<?php

/**
 * 基础库管理
 *企业管理-新增企业
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */

namespace app\api\controller\activemanage;

use think\Controller;
use PhpOffice\PhpWord\TemplateProcessor;
use app\api\controller\common\CommonPlus;
// use think\console\command\make\Controller;
use think\Db;
class Companymgr1 extends Controller
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companymgr');
    }

    /**
     * 企业列表
     */
    public function companyList()
    {


        //$keyword = $this->request->param('keyword');
        $page = $this->request->param('page');
        $where = array();
        $page = max(1, intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0; //回头问下



        // 查询 st
        $compname = $this->request->param('compname');
        $category = $this->request->param('category');
        $second_category = $this->request->param('second_category');
        $intime = $this->request->param('intime');
        $endtime = $this->request->param('endtime');
        $addr_person = $this->request->param('addr');
        $product = $this->request->param('product'); // 产品有点疑问

        $keyword = "";
        // and 
        if (!empty($compname)) {
            $keyword = $compname;
            $where['company_name'] = array('like', '%' . $keyword . '%');
        }
        if (!empty($category)) {

            //分类 不知道传的是数字还是  汉字
            $keyword = $category;
            // $where['category_id|second_category_id'] = $keyword;
            $where['category_id|second_category_id'] = array('like', '%' . $keyword . '%');
        }
        if (!empty($second_category)) {

            //分类 不知道传的是数字还是  汉字
            $keyword = $second_category;
           // $where['second_category_id'] = array('like', '%' . $keyword . '%');
        }


        if (!empty($intime) && !empty($endtime)) {
            // $where['create_time'] = ['between time', [strtotime($intime), strtotime($endtime)]];
            // $where['create_time'] = array(array('egt', strtotime($intime)), array('elt', strtotime($endtime)));
            $where['create_time'] = array(array('egt', strtotime($intime)), array('lt', strtotime("{$endtime} +1 day")));
        }
        if (!empty($addr_person)) {
            $keyword = $addr_person; //这个地址是啥字段？
            $where['province|area_city|area_town'] = array('like', '%' . $keyword . '%');
        }

        if (!empty($product)) {
            $keyword = $product;
            $where['product'] = array('like', '%' . $keyword . '%');
        }

        // 条件查询 ed



        //    var_dump($where);die;
        if (!empty($keyword)) {
            //  $where['company_name|company_name_en'] = array('like','%'.$keyword.'%');
            //   $where['company_name'] = array('like','%'.$keyword.'%');
        }

        $list = $this->model->listWherePage($where, 'id desc', ($page - 1) * $pagesize, $pagesize);

        //$total = count($list) ;
        $total = $this->model->countWhere($where);
        //var_dump($list);die;
        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list, 'total' => $total, 'countpage' => ceil($total / $pagesize)]]);

    }



    /**
     * 企业列表 内部调用 
     * 
     */
    public function companyListall()
    {


        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');
        $where = array();
        $page = max(1, intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0; //回头问下

        if (!empty($keyword)) {
            $where['company_name|company_name_en'] = array('like', '%' . $keyword . '%');
        }

        $list = $this->model->listAll($where, 'id desc', ($page - 1) * $pagesize, $pagesize);
        // $total = $this->model->countWhere($where);
        $total = 1;
        //return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total,'countpage'=>ceil($total/$pagesize)]]);
        return $list;
    }

    /**
     * 企业基本信息
     * @return \think\response\Json|void
     */
    public function companyDetail()
    {
        $id = $this->request->param('id');

        $where['id'] = $id;
        //$where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code' => 1, 'msg' => 'success', 'data' => $info]);
    }

    /**
     * 新增企业
     * @return \think\response\Json|void
     * 1：有基础库，企业库。  暂时先这么搞
     */
    public function addCompany()
    {
        //==common
        $create_uid  = $this->admin['id'];

        $selfcontent = $this->request->param('selfcontent'); //自定义表单 理论上只有新增 才有 。。让前台隐藏吧

        //
        $company_name = $this->request->param('company_name'); //公司中文名
        $company_name_en = $this->request->param('company_name_en'); //    
        $company_describe = $this->request->param('company_describe'); //简介   
        $tel = $this->request->param('tel');   //固定电话
        $link = $this->request->param('link');
        $product_brand = $this->request->param('product_brand');   //产品品牌
        $application_area = $this->request->param('application_area');    //应用领域  字段？？ 有两个
        $mobile = $this->request->param('mobile');
        $email = $this->request->param('email');
        $qq = $this->request->param('qq');
        $wechat = $this->request->param('wechat');
        $msn = $this->request->param('msn');
        $product_describe = $this->request->param('product_describe');   //产品介绍
        $product_positioning = $this->request->param('product_positioning');  //定位
        $product_keyword = $this->request->param('product_keyword');    //关键字
        //$application_area = $this->request->param('application_area');  //应用领域 下拉框？还是？？ 两个字段页面
        $remark = $this->request->param('remark');
        $category_id = $this->request->param('category_id');  //一级 分类
        $second_category_id = $this->request->param('second_category_id');     //二级 分类
        $product = $this->request->param('product');  //产品
        $capacitys = $this->request->param('capacitys');  //年产能
        $sale_fee = $this->request->param('sale_fee'); //年销售总额:
        $sale_pb = $this->request->param('sale_pb'); //pb年PB销售占比
        $pb_time = $this->request->param('pb_time');  //进入pb时间
        $pb_customer = $this->request->param('pb_customer'); //主要pb客户
        $country = $this->request->param('country');
        $country_code = $this->request->param('country_code'); //国家代码
        $province = $this->request->param('province');  //省份
        $area = $this->request->param('area'); //区域
        $area_city = $this->request->param('area_city');  //地级市
        $area_town = $this->request->param('area_town'); //县市      
        // 个人信息
        $surname_cn = $this->request->param('surname_cn');  //中文 姓
        $surname_en = $this->request->param('surname_en');  //英文 姓
        $name_cn = $this->request->param('name_cn');     // 中文名
        $name_en = $this->request->param('name_en'); // 英文名
        $call_name_cn = $this->request->param('call_name_cn'); // 称谓 中
        $call_name_en = $this->request->param('call_name_en'); //称谓 英文
        $positions_cn = $this->request->param('positions_cn');  // 职务 中
        $positions_en = $this->request->param('positions_en'); //职务
        $department_cn = $this->request->param('department_cn'); // 部门中
        $department_en = $this->request->param('department_en'); //部门 英
        $tel_person_code = $this->request->param('tel_person_code');  // 区号
        $tel_person = $this->request->param('tel_person'); //
        $tel_person_history = $this->request->param('tel_person_history'); //原始号码
        $mobile_person = $this->request->param('mobile_person'); //个人手机         
        $fax_person = $this->request->param('fax_person');  // 传真
        $fax_history = $this->request->param('fax_history');  // 原始传真
        $email_person = $this->request->param('email_person');  // email 个人
        $mobile_status = $this->request->param('mobile_status');  // 电话已核对 是需要填写数字？？
        $addr_person = $this->request->param('addr_person');  // 地址 个人
        $info_level = $this->request->param('info_level'); // 信息质量 
        $data_from = $this->request->param('data_from');  // 数据来源
        $duties = $this->request->param('duties');  // 岗位职责
        $job_nature = $this->request->param('job_nature');  // 工作性质
        $company_ID = $this->request->param('company_ID'); //公司id 
        $persontype = $this->request->param('persontype'); //人员类别
        $industrys = $this->request->param('industrys'); // 行业           
        $company_nature = $this->request->param('company_nature');  // 公司性质 

        $qualifications = $this->request->param('qualifications');  // 资质 
        $company_form = $this->request->param('company_form');  // 公司形态      
        $business = $this->request->param('business');  // 业务性质
        $system_status = $this->request->param('system_status');  // 系统状态
        $id_card = $this->request->param('id_card');  // 证件号码
        $chau = $this->request->param('chau');  // 所属洲
        $ishzselfbrander = $this->request->param('ishzselfbrander');  // 是否合作过自有品牌客户
        $ishzselfbrand = $this->request->param('ishzselfbrand');  // 是否合作过自有品牌    
        $label = $this->request->param('label');  //   
        $label_2 = $this->request->param('label_2');  //   


        $data = array(
            'create_uid' => $create_uid, 'remark' => $remark, 'update_time' => time(),'create_time' => time(), 'company_name' => $company_name,
            'company_name_en' => $company_name_en, 'company_describe' => $company_describe, 'tel' => $tel, 'link' => $link,
            'product_brand' => $product_brand, 'application_area' => $application_area, 'mobile' => $mobile,
            'email' => $email, 'qq' => $qq, 'wechat' => $wechat, 'msn' => $msn, 'product_describe' => $product_describe,
            'product_positioning' => $product_positioning, 'product_keyword' => $product_keyword, 'remark' => $remark,
            'category_id' => $category_id, 'second_category_id' => $second_category_id, 'product' => $product,
            'capacitys' => $capacitys, 'sale_fee' => $sale_fee, 'sale_pb' => $sale_pb, 'pb_time' => $pb_time,
            'pb_customer' => $pb_customer, 'country' => $country, 'country_code' => $country_code, 'province' => $province,
            'area' => $area, 'area_city' => $area_city, 'area_town' => $area_town,
            'surname_cn' => $surname_cn, 'name_cn' => $name_cn, 'surname_en' => $surname_en,
            'name_en' => $name_en, 'call_name_cn' => $call_name_cn, 'call_name_en' => $call_name_en,
            'positions_cn' => $positions_cn, 'positions_en' => $positions_en, 'department_cn' => $department_cn,
            'department_en' => $department_en, 'tel_person_code' => $tel_person_code, 'tel_person' => $tel_person,
            'tel_person_history' => $tel_person_history, 'mobile_person' => $mobile_person, 'fax_person' => $fax_person,
            'fax_history' => $fax_history, 'email_person' => $email_person, 'mobile_status' => $mobile_status,
            'addr_person' => $addr_person, 'info_level' => $info_level, 'data_from' => $data_from,
            'duties' => $duties, 'job_nature' => $job_nature, 'company_ID' => $company_ID,
            'persontype' => $persontype, 'industrys' => $industrys, 'company_nature' => $company_nature, 'qualifications' => $qualifications,
            'company_form' => $company_form, 'business' => $business, 'system_status' => $system_status,
            'id_card' => $id_card, 'chau' => $chau, 'ishzselfbrander' => $ishzselfbrander, 'ishzselfbrand' => $ishzselfbrand, 'selfcontent' => $selfcontent,
            'label' => $label, 'label_2' => $label_2
        );


        // 验证电话 企业  排除 删除
        if (!empty($company_name)) {
            $where = ['company_name' => $company_name, 'is_delete' => 0];
            $ckinfo = $this->model->countWhere($where);
            $msg = "公司名已存在!";

            if ($ckinfo > 0) {
                return json(['code' => 0, 'msg' => $msg]);
            }
        }

        if (!empty($tel)) {
            $where = ['tel' => $tel, 'is_delete' => 0];
            $ckinfo = $this->model->countWhere($where);
            $msg = "公司固定电话号码已存在!";

            if ($ckinfo > 0) {
                return json(['code' => 0, 'msg' => $msg]);
            }
        }
        if (!empty($mobile)) {
            $where = ['mobile' => $mobile, 'is_delete' => 0];
            $ckinfo = $this->model->countWhere($where);
            $msg = "公司手机号码已存在!";

            if ($ckinfo > 0) {
                return json(['code' => 0, 'msg' => $msg]);
            }
        }


        //  Yanzheng ed   适当放松的



        // var_dump ($data);die;
        $r = $this->model->insertData($data);
        if (!$r) {
            return json(['code' => 0,'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1,'data'=> $r, 'msg' => '操作成功']);
    }


    public function editCompany()
    {
        $com = new CommonPlus();
        // com
        $id = $this->request->param('id'); //必须
        $create_uid  = $this->admin['id'];
        $selfcontent = $this->request->param('selfcontent'); //自定义表单 理论上只有新增 才有 。。让前台隐藏吧
        // com 
 
        //==========

        $company_name = $this->request->param('company_name'); //公司中文名
        $company_address = $this->request->param('company_address'); //地址   
        $company_name_en = $this->request->param('company_name_en'); //    公司英文文名
        $company_describe = $this->request->param('company_describe'); //简介   
        $tel = $this->request->param('tel');   //固定电话
        $link = $this->request->param('link');
        $product_brand = $this->request->param('product_brand');   //产品品牌
        $application_area = $this->request->param('application_area');    //应用领域  字段？？ 有两个
        $mobile = $this->request->param('mobile');
        $email = $this->request->param('email');
        $qq = $this->request->param('qq');
        $wechat = $this->request->param('wechat');
        $msn = $this->request->param('msn');
        $product_describe = $this->request->param('product_describe');   //产品介绍
        $product_positioning = $this->request->param('product_positioning');  //定位
        $product_keyword = $this->request->param('product_keyword');    //关键字
        //$application_area = $this->request->param('application_area');  //应用领域 下拉框？还是？？ 两个字段页面
        $remark = $this->request->param('remark');
        $category_id = $this->request->param('category_id');  //一级 分类
        $second_category_id = $this->request->param('second_category_id');     //二级 分类
        $product = $this->request->param('product');  //产品
        $capacitys = $this->request->param('capacitys');  //年产能
        $sale_fee = $this->request->param('sale_fee'); //年销售总额:
        $sale_pb = $this->request->param('sale_pb'); //pb年PB销售占比
        $pb_time = $this->request->param('pb_time');  //进入pb时间
        $pb_customer = $this->request->param('pb_customer'); //主要pb客户
        $country = $this->request->param('country');  //国家
        $country_code = $this->request->param('country_code'); //国家代码
        $area = $this->request->param('area'); //区域
        $province = $this->request->param('province');  //省份
        $area_city = $this->request->param('area_city');  //地级市
        $area_town = $this->request->param('area_town'); //县市      
        // 个人信息
        $surname_cn = $this->request->param('surname_cn');  //中文 姓
        $surname_en = $this->request->param('surname_en');  //英文 姓
        $name_cn = $this->request->param('name_cn');     // 中文名
        $name_en = $this->request->param('name_en'); // 英文名
        $call_name_cn = $this->request->param('call_name_cn'); // 称谓 中
        $call_name_en = $this->request->param('call_name_en'); //称谓 英文
        $positions_cn = $this->request->param('positions_cn');  // 职务 中
        $positions_en = $this->request->param('positions_en'); //职务
        $department_cn = $this->request->param('department_cn'); // 部门中
        $department_en = $this->request->param('department_en'); //部门 英
        $tel_person_code = $this->request->param('tel_person_code');  // 区号
        $tel_person = $this->request->param('tel_person'); // 电话
        $tel_person_history = $this->request->param('tel_person_history'); //原始号码
        $mobile_person = $this->request->param('mobile_person'); //个人手机         
        $fax_person = $this->request->param('fax_person');  // 传真
        $fax_history = $this->request->param('fax_history');  // 原始传真
        $email_person = $this->request->param('email_person');  // email 个人
        $mobile_status = $this->request->param('mobile_status');  // 电话已核对 是需要填写数字？？
        $addr_person = $this->request->param('addr_person');  // 地址 个人
        $info_level = $this->request->param('info_level'); // 信息质量 
        $data_from = $this->request->param('data_from');  // 数据来源
        $duties = $this->request->param('duties');  // 岗位职责
        $job_nature = $this->request->param('job_nature');  // 工作性质
        $company_ID = $this->request->param('company_ID'); //公司id 
        $persontype = $this->request->param('persontype'); //人员类别
        $industrys = $this->request->param('industrys'); // 行业           
        $company_nature = $this->request->param('company_nature');  // 公司性质 
        $qualifications = $this->request->param('qualifications');  // 资质 
        $company_form = $this->request->param('company_form');  // 公司形态      
        $business = $this->request->param('business');  // 业务性质
        $system_status = $this->request->param('system_status');  // 系统状态
        $id_card = $this->request->param('id_card');  // 证件号码
        $chau = $this->request->param('chau');  // 所属洲
        $ishzselfbrander = $this->request->param('ishzselfbrander');  // 是否合作过自有品牌客户
        $ishzselfbrand = $this->request->param('ishzselfbrand');  // 是否合作过自有品牌    
        $label = $this->request->param('label');  //   
        $label_2 = $this->request->param('label_2');  //  

        $data = array(
            'create_uid' => $create_uid, 'update_time' => time(), 'selfcontent' => $selfcontent, 'remark' => $remark, 'company_name' => $company_name,
            'company_name_en' => $company_name_en, 'company_describe' => $company_describe, 'tel' => $tel, 'link' => $link,
            'product_brand' => $product_brand, 'application_area' => $application_area, 'mobile' => $mobile,
            'email' => $email, 'qq' => $qq, 'wechat' => $wechat, 'msn' => $msn, 'product_describe' => $product_describe,
            'product_positioning' => $product_positioning, 'product_keyword' => $product_keyword, 'remark' => $remark,
            'category_id' => $category_id, 'second_category_id' => $second_category_id, 'product' => $product,
            'capacitys' => $capacitys, 'sale_fee' => $sale_fee, 'sale_pb' => $sale_pb, 'pb_time' => $pb_time,
            'pb_customer' => $pb_customer, 'country' => $country, 'country_code' => $country_code, 'province' => $province,
            'area' => $area, 'area_city' => $area_city, 'area_town' => $area_town,
            'surname_cn' => $surname_cn, 'name_cn' => $name_cn, 'surname_en' => $surname_en,
            'name_en' => $name_en, 'call_name_cn' => $call_name_cn, 'call_name_en' => $call_name_en,
            'positions_cn' => $positions_cn, 'positions_en' => $positions_en, 'department_cn' => $department_cn,
            'department_en' => $department_en, 'tel_person_code' => $tel_person_code, 'tel_person' => $tel_person,
            'tel_person_history' => $tel_person_history, 'mobile_person' => $mobile_person, 'fax_person' => $fax_person,
            'fax_history' => $fax_history, 'email_person' => $email_person, 'mobile_status' => $mobile_status,
            'addr_person' => $addr_person, 'info_level' => $info_level, 'data_from' => $data_from,
            'duties' => $duties, 'job_nature' => $job_nature, 'company_ID' => $company_ID,
            'persontype' => $persontype, 'industrys' => $industrys, 'company_nature' => $company_nature, 'qualifications' => $qualifications,
            'company_form' => $company_form, 'business' => $business, 'system_status' => $system_status,
            'id_card' => $id_card, 'chau' => $chau, 'ishzselfbrander' => $ishzselfbrander, 'ishzselfbrand' => $ishzselfbrand,
            'label' => $label, 'label_2' => $label_2,'company_address'=>$company_address
        );
        //=======

        //检测是不是 当前公司名称
        $where2['id'] = $id;
        $where2['is_delete'] = 0;
        $info2 = $this->model->findWhere($where2);

        // 验证电话 企业  排除 删除
        if (!empty($company_name)) {
            if ($info2['company_name'] == $company_name) {
            } else {
                $where1 = ['company_name' => $company_name, 'is_delete' => 0];
                $ckinfo = $this->model->countWhere($where1);
                $msg = "公司名已存在!";

                if ($ckinfo > 0) {
                    return json(['code' => 0, 'msg' => $msg]);
                }
            }
        }

        if (!empty($tel)) {
            if ($info2['tel'] == $tel) {
            } else {
                $where1 = ['tel' => $tel, 'is_delete' => 0];
                $ckinfo = $this->model->countWhere($where1);
                $msg = "公司固定电话号码已存在!";

                if ($ckinfo > 0) {
                    return json(['code' => 0, 'msg' => $msg]);
                }
            }
        }

        if (!empty($mobile)) {
            if ($info2['mobile'] == $mobile) {
            } else {
                $where1 = ['mobile' => $mobile, 'is_delete' => 0];
                $ckinfo = $this->model->countWhere($where1);
                $msg = "公司手机号码已存在!";

                if ($ckinfo > 0) {
                    return json(['code' => 0, 'msg' => $msg]);
                }
            }
        }




        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data, $where);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        /**
         * 企业管理 操作日志
         */
 
        $datalog=array('admin_id'=>$this->admin['id'],'admin_name'=>$this->admin['realname'],'content'=>'修改：ID为'.$id."表单内容",'ip' => request()->ip(),'create_time' => time());
        $wherelog=array('create_time' => time());
        $tbname="company_edlog";       
        $re_log=$com->insert_com_fxlist($datalog, $wherelog, $tbname);

        return json(['code' => 1, 'msg' => '操作成功']);
    }


    /**
     * 删除项目
     */
    public function delCompany()
    {
        $id = $this->request->param('id');

        $where = ['id' => $id, 'is_delete' => 0];
        $r = $this->model->countWhere($where);

        if (empty($r)) {
            return json(['code' => 0, 'msg' => '记录不存在，请核实']);
        }

        $r = $this->model->deleteWhere(['id' => $id]);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 企业管理批量 删除项目
     */
    public function delMoreCompany()
    {
        $id = $this->request->param('id');
        $create_uid  = $this->admin['id'];
        $map['id'] = array('in', explode(',', $id));
        $r = $this->model->deleteWhere($map);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 获取此页面所有下拉
     *params 要查询的表名 ，主级需预先存表，防止错误不返回
     */

    public function getQyFormAllxla()
    {
        $comp = new CommonPlus();
        $params = array('tbarrs' => array('xiala_gsxz', 'xiala_gsxt', 'xiala_gslevel', 'xiala_sshy', 'xiala_ywxz'));

        $list = $comp->selAll_xla_BytypeF1('gsxz', $params);
        if (!$list) {
            return json(['code' => 0, 'msg' => 'error', 'data' => ['list' => $list]]);
        }
        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list]]);
    }
    /**
     * 查询 log 编辑日志 sel_com_fxlist
     */
    public function getlog_edit()
    {
        $comp = new CommonPlus();
        $params = array('tbarrs' => array('xiala_gsxz', 'xiala_gsxt', 'xiala_gslevel', 'xiala_sshy', 'xiala_ywxz'));
        $data="";
        $where['id'] = array('gt',0); 
        $tbname="company_edlog";
        $list = $comp->sel_com_fxlist($data, $where, $tbname);
        if (!$list) {
            return json(['code' => 0, 'msg' => 'error', 'data' => ['list' => $list]]);
        }
        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list]]);
    }


    //===导出
    public function export()
    {

        $userid = "";
        $type = "";
        $usersub = "";
        $cstname = "";
        $seldept = "";
        $sttime = "";
        $edtime = "";
        //$whereH = array('userid' => '149', 'reportdate' => '20180427');
        // $whereH = array('type' => $type, 'departmy' => $departmy, 'datestar' => $datestar, 'dateend' => $dateend);
        $search_data = array(
            'type' => $type,
            'userid' => $userid,
            'usersub' => $usersub,
            'cstname' => $cstname,
            'seldept' => $seldept,
            'sttime' => $sttime,
            'edtime' => $edtime
        );

        $columns = array(
            //array('title' => '员工ID', 'field' => 'ordersn', 'width' => 24),
            array('title' => '公司名称', 'field' => 'realname', 'width' => 12),
            //array('title' => '计划类别', 'field' => 'realname', 'width' => 12),
            array('title' => '地址', 'field' => 'ordersn', 'width' => 24),
            array('title' => '产品定位', 'field' => 'ordersn', 'width' => 24),
            array('title' => '产品', 'field' => 'ordersn', 'width' => 24),
            array('title' => '联系人', 'field' => 'realname', 'width' => 12),
            // array('title' => '公司名称英文', 'field' => 'mobile', 'width' => 12),
            // array('title' => '固定电话', 'field' => 'goods_title', 'width' => 24)

        );
        $title = "企业明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        $daily = $this->doexport($columns, $filename, $search_data);
        exit;
    }

    //common xsport
    public function doexport($datas, $filename, $sort)
    {
        $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";

        $str .= "<table border=1>";
        $condition = "";
        if ($sort['usersub'] != "") {
            $condition .= " and d.username='" . $sort['usersub'] . "' ";
        }
        if ($sort['cstname'] != "") {
            $condition .= " and coustname like'%" . $sort['cstname'] . "%' ";
        }
        if ($sort['sttime'] != "") {
            $condition .= " and (DATE_FORMAT(d.dateSign,'%Y/%m/%d')>='" . $sort['sttime'] . "' and DATE_FORMAT(d.dateSign,'%Y/%m/%d') <='" . $sort['edtime'] . "') ";
        }

        // $sql="select  d.*,u.dept from wp_wsmanage_salecontract d
        //     inner join wp_user u on u.id=d.userid
        //     where d.id !='' $condition order by d.id desc ";

        $where = array();   
        
        $page = $this->request->param('page');
        $page = max(1, intval($page));
        $pagesize =2000;
        $where['is_delete'] = 0; //回头问下
        
        $company_name = $this->request->param('company_name'); // 公司名称
        $ids = $this->request->param('ids'); // 单独勾选某个数据
        if($ids){
            $where['id'] = array('in', explode(',', $ids));
        }else{
            if($company_name !=""){
                $where['company_name|company_name_en'] = array('like', '%' . $company_name . '%');
            }

        }

        // var_dump($where);die;

       // var_dump($keyword);die;       

        $list = $this->model->listAll($where, 'id desc', ($page - 1) * $pagesize, $pagesize);    

    
        // 需要筛选  后期做吧 03222 选择
      //  $list = $this->companyListall(); // 这个好像也是 筛选作用

        // var_dump($list);die; 
        $tht = "<tr>"; //biao tr
        foreach ($datas as $key => $trno) {
            $tht .= "<th>{$trno['title']}</th>";
        }
        $tht .= "</tr>";
        $str .= $tht;
        foreach ($list as $key) {

            $str .= "<tr>";
            $str .= "<td>$key->company_name</td>";
            // if ($key->company_name == "18") {
            //     $str .= "<td>销售一部</td>";
            // } else {
            //     $str .= "<td>2222</td>";
            // }

            $str .= "<td>$key->company_address</td>";
            $str .= "<td>$key->product_positioning</td>";
            $str .= "<td>$key->product</td>";
            $str .= "<td>$key->contact</td>";
            $str .= "</tr>";
        }

        $str .= "</table></body></html>";
        header("Content-Type: application/vnd.ms-excel; name='excel'");   #类型
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header("Expires: 0");
        exit($str);
    }
    //===导出 ed

    /**
     * 导入  
     * 获取路径 直接use
     */

    public function import5()
    {
        $filename = input('post.filename'); // 获取 路径
        //$file是上传到服务器后的excel文件路径
        $path = ROOT_PATH . 'public/uploads/excel/' . $filename;
        // $path=ROOT_PATH.'public/static/test.xls';
        $file = $path;
        //判断excel文件格式
        $type = pathinfo($file);
        $type = strtolower($type["extension"]);
        if ($type == 'xlsx') {
            $type = 'Excel2007';
        } elseif ($type == 'xls') {
            $type = 'Excel5';
        }
        $objReader = \PHPExcel_IOFactory::createReader($type);
        $objPHPExcel = $objReader->load($file, $encode = 'utf-8');
        $excel_array = $objPHPExcel->getsheet(0)->toArray();
        $res = [];
        array_shift($excel_array); //去掉第一行，因为我上传的excel文件第一行为列名
        if (!is_array($excel_array) || empty($excel_array)) {
            return $res;
        } else {
            //循环读取数据
            foreach ($excel_array as $key => $value) {
                $res[$key]['company_name'] = (string) $value[0];
                $res[$key]['company_address'] = (string) $value[1];
                $res[$key]['product_positioning'] = (string) $value[2]; // 产品定位
                $res[$key]['product'] = (string) $value[3]; // 产品
                $res[$key]['contact'] = (string) $value[4]; // 网址
                // $res[$key]['contact'] = (string)$value[5]; // 联系人
                $res[$key]['create_time'] = time(); // 网址
                $res[$key]['comfrom'] = "线下导入"; // 网址 

            }
        }

        // 多循环了
        return $this->dodaorudata($res);  // 要加return 

    }
    // 执行导表 公司 表
    public function dodaorudata($res)
    {
        $a = 0;
        foreach ($res as $item) {

            $r = $this->model->insertData($item);
            if (!$r) {
                return json(['code' => 0, 'msg' => '操作失败，请重试']);
            }

            $a++;
        }
        // var_dump( $a);die;

        if (intval($a) > 1) {
            return json(['code' => 1, 'msg' => '操作成功']);
            die;
        } else {

            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    // ===导入  ed




    /**
     * 企业列表 ***********
     * 筛选用 应该是 展会那块需要。 缺的字段 补上吧。暂时 把重要的    放上了
     */
    public function companyList_shaixuan()
    {


        //$keyword = $this->request->param('keyword');
        $page = $this->request->param('page');
        $where = array();
        $page = max(1, intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0; //回头问下

        // 查询 st 精确查询还是模糊 查询。。。。擦  都是必要的字段留着 其他的没卵用 
        $company_name = $this->request->param('company_name'); //公司中文名 
        $tel = $this->request->param('tel');   //固定电话
        $link = $this->request->param('link');
        $product_brand = $this->request->param('product_brand');   //产品品牌
        $application_area = $this->request->param('application_area');    //应用领域  字段？？ 有两个
        $mobile = $this->request->param('mobile');
        $email = $this->request->param('email');
        $product_describe = $this->request->param('product_describe');   //产品介绍
        $product_positioning = $this->request->param('product_positioning');  //定位
        $product_keyword = $this->request->param('product_keyword');    //关键字 
        $category_id = $this->request->param('category_id');  //一级 分类
        $second_category_id = $this->request->param('second_category_id');     //二级 分类
        $product = $this->request->param('product');  //产品    
        $pb_time = $this->request->param('pb_time');  //进入pb时间
        $pb_customer = $this->request->param('pb_customer'); //主要pb客户
        $country = $this->request->param('country');
        $country_code = $this->request->param('country_code'); //国家代码
        $province = $this->request->param('province');  //省份
        $area = $this->request->param('area'); //区域
        $area_city = $this->request->param('area_city');  //地级市
        $area_town = $this->request->param('area_town'); //县市  

        $keyword = "";
        // and 
        if (!empty($company_name)) {
            // 公司名模糊 其他没必要
            $keyword = $company_name;
            $where['company_name'] = array('like', '%' . $keyword . '%');
        }
        if (!empty($link)) {
            $keyword = $link;
            $where['link'] = $keyword;
        }
        if (!empty($product_brand)) {   //品牌
            $keyword = $product_brand;
            $where['product_brand'] = $keyword;
        }

        if (!empty($mobile)) {
            $keyword = $mobile;
            $where['mobile'] = $keyword;
        }

        if (!empty($email)) {
            $keyword = $email;
            $where['email'] = $keyword;
        }

        if (!empty($product_positioning)) {
            $keyword = $product_positioning;
            $where['product_positioning'] = $keyword;
        }

        if (!empty($product_keyword)) {
            $keyword = $product_keyword;
            $where['product_keyword'] = $keyword;
        }

        if (!empty($category_id)) {
            //一级 分类    //不知道传的是数字还是  汉字
            $keyword = $category_id;
            $where['category_id'] = $keyword;
        }

        if (!empty($second_category_id)) {
            //2级 分类   //不知道传的是数字还是  汉字
            $keyword = $second_category_id;
            $where['second_category_id'] = $keyword;
        }
        if (!empty($product)) {
            // 
            $keyword = $product;
            $where['product'] = $keyword;
        }

        if (!empty($country)) {
            //国家 
            $keyword = $country;
            $where['country'] = $keyword;
        }


        if (!empty($country_code)) {
            //国家代码 
            $keyword = $country_code;
            $where['country_code'] = $keyword;
        }

        if (!empty($province)) {
            //省份 
            $keyword = $province;
            $where['province'] = $keyword;
        }

        if (!empty($area)) {
            //区域 
            $keyword = $area;
            $where['area'] = $keyword;
        }


        if (!empty($area_city)) {
            //地级市 
            $keyword = $area_city;
            $where['area_city'] = $keyword;
        }

        if (!empty($area_town)) {
            //县市  
            $keyword = $area_town;
            $where['area_town'] = $keyword;
        }



        if (!empty($intime) && !empty($endtime)) {
            //$where['create_time'] = array(array('gt', strtotime($intime)), array('lt', strtotime($endtime)));
        }



        // 条件查询 edf fffff


        // $list = $this->model->listWherePage($where, 'id desc', ($page - 1) * $pagesize, $pagesize);
        // 按道理 方法之间调用 不需要分页
        $list = $this->model->listAll($where, 'id desc', ($page - 1) * $pagesize, $pagesize);
        $total = $this->model->countWhere($where);

        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list, 'total' => $total, 'countpage' => ceil($total / $pagesize)]]);
    }

    // ces

    public function test1()
    {


        //==common
        $create_uid  = $this->admin['id'];
        //
        $company_name = 11; //公司中文名

        $company_name_en = 2; //    
        $company_describe = 3; //简介             

        $data = array(
            'create_time' => time(), 'company_name' => $company_name,
            'company_name_en' => $company_name_en, 'company_describe' => $company_describe

        );
        $data1 = array(
            'id' => 10, 'content' => "fffff"

        );

        // var_dump ($data);die;
        // $r = $this->model->insertDataf1($data);

        $comp = new CommonPlus();
        $params = array('tbarrs' => array('xiala_gsxz', 'xiala_gsxt', 'xiala_gslevel', 'xiala_sshy', 'xiala_ywxz'));
        $r = $comp->selAll_xla_BytypeF1('gsxz', $params);
        var_dump($r);
        die;
        // active('模块/控制器/方法',[参数一,参数二......])
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

}
